package com.demo.cal;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.demo.cal.databinding.ActivityMainBinding;

import java.math.BigDecimal;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private boolean isStart;

    private com.demo.cal.databinding.ActivityMainBinding mInflate;

    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (isStart) {
                cal();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflate = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mInflate.getRoot());
        mInflate.btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeInputMethod();
                cal();
            }
        });
        mInflate.etFoodCost.addTextChangedListener(mTextWatcher);
        mInflate.etStuffCost.addTextChangedListener(mTextWatcher);
        mInflate.etProfitRate.addTextChangedListener(mTextWatcher);
        mInflate.etLossRate.addTextChangedListener(mTextWatcher);
        mInflate.etFoodWeight.addTextChangedListener(mTextWatcher);
    }

    private void cal() {
        Editable text = mInflate.etFoodCost.getText();
        if (checkEdit(text)) {
            toast("请输入成本");
            return;
        }
        text = mInflate.etStuffCost.getText();
        if (checkEdit(text)) {
            toast("请输入调料成本0%-100%");
            return;
        }
        text = mInflate.etProfitRate.getText();
        if (checkEdit(text)) {
            toast("请输入利润率0%-100%");
            return;
        }
        text = mInflate.etLossRate.getText();
        if (checkEdit(text)) {
            toast("请输入预计损耗率0%-100%");
            return;
        }
        text = mInflate.etFoodWeight.getText();
        if (checkEdit(text)) {
            toast("请输入成餐总重量");
            return;
        }
        isStart = true;
        result();
    }

    private void result() {
        StringBuffer buffer = new StringBuffer();
        String trim = mInflate.etFoodCost.getText().toString().trim();
        double foodCost = Double.parseDouble(trim);
        buffer.append("采购菜品成本：\t\t" + trim);
        buffer.append("\n");
        buffer.append("\n");

        String stuff = mInflate.etStuffCost.getText().toString().trim();
        double stuffCost = Double.parseDouble(stuff);

        double value = (foodCost * (1 + stuffCost / 100));
        buffer.append("加入材料成本：\t\t" + format(value));
        buffer.append("\n");
        buffer.append("\n");

        String profit = mInflate.etProfitRate.getText().toString().trim();
        double profitRate = Double.parseDouble(profit);

        value = (value * (1 + profitRate / 100));
        buffer.append("利润率" + profit + "%后：\t\t" + format(value));
        buffer.append("\n");
        buffer.append("\n");

        String loss = mInflate.etLossRate.getText().toString().trim();
        double lossRate = Double.parseDouble(loss);

        value = (value / (1 - lossRate / 100));
        buffer.append("预计损耗" + loss + "%：\t\t" + format(value));
        buffer.append("\n");
        buffer.append("\n");

        String food = mInflate.etFoodWeight.getText().toString().trim();
        double foodWeight = Double.parseDouble(food);
        buffer.append("成品菜品重量:\t\t\t" + format(foodWeight) + "Kg");
        buffer.append("\n");
        buffer.append("\n");
        value = value / (foodWeight * 1000) * 50;
        buffer.append("菜品单价50g：\t\t" + format(value) + "元");
        buffer.append("\n");
//        菜品成本：100\n 加入调料燃器 110 \n 利润率50%后 165\n 损耗值30% 235 \n菜品重量20kg \n菜品单价50g为0.58

        mInflate.tvMessage.setText(buffer.toString());
    }

    //直接关闭键盘输入法
    private void closeInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            // imm.toggleSoftInput(0,
            // InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
            imm.hideSoftInputFromWindow(mInflate.getRoot().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    private String format(Double price) {
        return String.format(Locale.CHINA, "%.2f", price);
    }

    private void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private boolean checkEdit(Editable text) {
        if (text == null) {
            return true;
        }
        String str = text.toString().trim();
        if (TextUtils.isEmpty(str)) {
            return true;
        }
        try {
            Double.parseDouble(str);
        } catch (Throwable e) {
            return true;
        }
//        if (!TextUtils.isDigitsOnly(str)) {
//            return true;
//        }
        return false;
    }
}